<?php
/**
* 消息数据模型
* @date: 2018年6月5日 上午10:12:44
* @author: xiaocui
*/

namespace app\admin666\model;

use think\Model;

class Member extends Model
{
    /**
     * 登录指定用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    public function login($username=null,$password=null){
        $map['username'] = $username;
        $map['group_id'] = ['notin','2'];
        
        /* 获取用户数据 */
        $user = $this->where($map)->find();
        if(!empty($user)){
            /* 检测是否在当前应用注册 */
            //$user = $this->field(true)->find($uid);
            if(!$user || 1 != $user['status']) {
                $this->error = '用户不存在或被禁用！'; //应用级别禁用
                return false;
            }
            /* 验证用户密码 */
            if(think_ucenter_md5($password) === $user['password']){
                $this->autoLogin($user); //更新用户登录信息
        
                //记录行为
                action_log('admin_login', 'member', $user['id'], $user['id']);
                return $user['id']; //登录成功，返回用户ID
            } else {
                $data['login_failed_count'] = ['inc',1];
                if($user['login_failed_count']>=4){
                    $data['status']=0;
                    $this->save($data, ['id'=>$user['id']]);
                    $this->error = '密码输入错误次数过多，请联系管理员解封！';
                }else{
                    $this->save($data, ['id'=>$user['id']]);
                    $this->error = '密码错误，您还有'.(4-$user['login_failed_count']).'次机会！';
                }
                return false; //密码错误
            }
        } else {
            $this->error = '用户不存在或被禁用！';
            return false; //用户不存在或被禁用
        }
    }
    
    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    private function autoLogin($user){
        /* 更新登录信息 */
        $data = [
            'login'           => ['inc',1],
            'last_login_time' => NOW_TIME,
            'last_login_ip'   => get_client_ip(1,true),
            'login_failed_count' => 0
        ];
        $this->save($data, ['id'=>$user['id']]);
    
        /* 记录登录SESSION和COOKIES */
        $auth = [
            'uid'             => $user['id'],
            'username'        => $user['username'],
            'last_login_time' => $user['last_login_time'],
        ];
        
        action_log(0,1,'Publics/login','管理员登录',$user['id']);
        session('user_auth', $auth);
        session('user_auth_sign', data_auth_sign($auth));
    
    }
    
    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        $us = session('user_auth');
        action_log(0,1,'Publics/logout','管理员退出登录',$us['uid']);
        session('user_auth', null);
        session('user_auth_sign', null);
    }
    
    /**
    * 修改密码
    * @date: 2018年6月6日 下午4:59:02
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function pwd(){
        $info = $this->get(UID);
        $newpass = input('post.password');
        $repass = input('post.repassword');
        $oldpass = think_ucenter_md5(input('post.oldpassword'));
        if($info['password']!=$oldpass){
            $this->error = '旧密码不正确！';
            return false;
        }
         
        if($newpass != $repass){
            $this->error = '两次输入的密布不一致！';
            return false;
        }
         
        $data['password'] = think_ucenter_md5(input('post.password'));
        if($this->save($data,['id'=>UID])){
            return true;
        }
        return false;
    }
    
    
    public function setPasswordAttr($value){
        return think_ucenter_md5($value);
    }
    
    
    /**
    * 充值金币
    * @date: 2018年6月13日 上午11:54:18
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function rechange($data,$isdl=false){
        if($isdl){ //给代理充值
            $info = $this->field('id,uid,golds')->where(['id'=>$data['id']])->find();
            if(empty($info)){
                return false;
            }
            
            if(intval($data['number']) <= 0){
                return false;
            }
            
            $this->save(['golds'=>['inc',$data['number']],'total'=>['inc',$data['number']]], ['id'=>$data['id']]);
            
            $data['uid'] = $info['uid'];
            $data['isdl'] = 1;
            $data['before_num'] = $info['golds'];
            
            if(model('CashLog')->log($data)){
                return true;
            }
            return false;
        }else{ //给玩家充值
            $data['isdl'] = 0;
            
            $res = rechange($data['uid'],$data['number'],true);
            if($res === true){
                $this->save(['golds'=>['dec',$data['number']]], ['id'=>UID]);
                if(model('CashLog')->log($data)){
                    //model('CashLog')->log($data);
                    return true;
                }
                return false;
            }
            
            return false;
        }
    }
    
    /**
    * 生成渠道号，渠道包，上传oss
    * @date: 2018年7月26日 下午6:09:05
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function make_cid($uid){
        $cid = '2'.$uid;
        $apk_url = make_apk($cid);
        $this->save(['cid'=>$cid,'apk_url'=>$apk_url],['id'=>$uid]);
    }
}